package com.chaoyong.higo.view;


import com.chaoyong.higo.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.view.animation.Animation.AnimationListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;

public class SelectPhotoesPopWin extends PopupWindow implements OnClickListener {
	
	private OnPhotoSelectedListener mlistener;
	private Context mContext;
	public View contentView;// root view
	private View pop_dialog_layout;
	private Button btn_take_man;
	private Button btn_pick_women;
	private Button btn_cancel;

	public static class Builder {

		private Context context;
		private OnPhotoSelectedListener listener;

		public Builder(Context context, OnPhotoSelectedListener listener) {
			this.context = context;
			this.listener = listener;
		}

		public SelectPhotoesPopWin build() {
			return new SelectPhotoesPopWin(this);
		}

	}

	public SelectPhotoesPopWin(Builder build) {
		this.mContext = build.context;
		this.mlistener = build.listener;
		initView();
	}
	
	private void initView() {
		contentView = LayoutInflater.from(mContext).inflate(R.layout.pop_select_photo, null);
		
		btn_take_man = (Button) contentView.findViewById(R.id.btn_take_man);
		btn_pick_women = (Button) contentView.findViewById(R.id.btn_pick_women);
		btn_cancel = (Button) contentView.findViewById(R.id.btn_cancel);
		pop_dialog_layout = contentView.findViewById(R.id.pop_dialog_layout);
		
		btn_take_man.setOnClickListener(this);
		btn_pick_women.setOnClickListener(this);
		btn_cancel.setOnClickListener(this);
		contentView.setOnClickListener(this);
		
		setTouchable(true);
		setFocusable(true);
		// setOutsideTouchable(true);
		setBackgroundDrawable(new BitmapDrawable());
		setAnimationStyle(R.style.FadeInPopWin);
		setContentView(contentView);
		setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
		setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
	}

	public interface OnPhotoSelectedListener {
		void onPhotoSelected(int which);
	}

	@Override
	public void onClick(View v) {
		if (v == contentView || v == btn_cancel) {
			dismissPopWin();
		}else if(v == btn_take_man){
			
			if (null != mlistener) {
				mlistener.onPhotoSelected(1);
			}
			
			dismissPopWin();
		}else if(v == btn_pick_women){
			
			if (null != mlistener) {
				mlistener.onPhotoSelected(0);
			}
			
			dismissPopWin();
		}
	}
	
	/**
	 * Show date picker popWindow
	 *
	 * @param activity
	 */
	public void showPopWin(Activity activity) {

		if (null != activity) {

			TranslateAnimation trans = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF,
					0, Animation.RELATIVE_TO_SELF, 1, Animation.RELATIVE_TO_SELF, 0);

			showAtLocation(activity.getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
			trans.setDuration(400);
			trans.setInterpolator(new AccelerateDecelerateInterpolator());

			pop_dialog_layout.startAnimation(trans);
		}
	}

	/**
	 * Dismiss date picker popWindow
	 */
	public void dismissPopWin() {

		TranslateAnimation trans = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0,
				Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1);

		trans.setDuration(400);
		trans.setInterpolator(new AccelerateInterpolator());
		trans.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {

			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation animation) {

				dismiss();
			}
		});

		pop_dialog_layout.startAnimation(trans);
	}

}
